package com.smt.modules.app.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.smt.common.base.BaseController;
import com.smt.common.utils.PageUtils;
import com.smt.common.utils.R;
import com.smt.modules.bus.entity.MessageHandleEntity;
import com.smt.modules.bus.service.MessageHandleService;
import com.smt.modules.bus.vo.MessageHandleVo;
import com.smt.modules.bus.vo.request.MessageReq;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 
 *
 * @author murong
 * @email 981620741@qq.com
 * @date 2018-11-16 11:47:30
 */
@RestController
@RequestMapping("app/messageHandle")
@Api(tags = {"MessageHandleController"},description = "消息处理(客户端接口)")
public class AppMessageHandleController extends BaseController{
    @Autowired
    private MessageHandleService messageHandleService;

    /**
     * 信息
     *
     * @param messageHandleVo
     * @return
     */
    @PostMapping("/unreadCount")
    @ApiOperation(value = "获取未读消息的条数")
    public R unreadCount(@RequestBody MessageHandleVo messageHandleVo){

        return messageHandleService.unreadCount(messageHandleVo);
    }

    /**
     * 查看详细消息
     *
     * @param id
     * @return
     */
    @GetMapping("/getMessages/{id}")
    @ApiOperation(value =  "查看消息列表",httpMethod = "GET")
    public R getMessages(@PathVariable("id") Long id){
        return messageHandleService.getMessages(id);
    }

    /**
     * 储存消息通知
     */
    @PostMapping("/save")
    @ApiOperation(value =  "储存消息通知",httpMethod = "POST")
    public R save(@RequestBody @Validated MessageReq message){
        //消息持久化
        messageHandleService.decoderMessage(message.getMessage());
        return R.ok();
    }

    /**
     * 读取消息
     *
     * @param id
     * @return
     */
    @PostMapping("/editMessage/{id}")
    @ApiOperation(value = "读取消息")
    public R editMessage(@PathVariable("id") Long id){
        MessageHandleEntity messageHandle = new MessageHandleEntity();
        messageHandle.setId(id);
        messageHandle.setStatus(1);
        boolean b = messageHandleService.updateById(messageHandle);
        return b?R.ok("读取消息成功"):R.error("读取消息失败");
    }

    /**
     * 删除消息
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteMessage/{id}")
    @ApiOperation(value = "修改消息状态",httpMethod = "DELETE")
    public R deleteMessage(@PathVariable("id") Long id){
        MessageHandleEntity messageHandle = new MessageHandleEntity();
        messageHandle.setId(id);
        messageHandle.setIsDelete(1);
        boolean b = messageHandleService.updateById(messageHandle);
        return b?R.ok("消息删除成功"):R.error("消息删除失败");
    }


    /**
     * 获取消息集合
     *
     * @param messageHandleVo
     * @return
     */
    @PostMapping("/getMessageList")
    @ApiOperation(value = "获取消息集合")
    public R getMessageList(@RequestBody MessageHandleVo messageHandleVo){
        return messageHandleService.getMessageList(messageHandleVo);
    }

    /**
     * 统一消息消息处理
     *
     * @param messageHandleVo
     * @return
     */
    @PostMapping("/clearMessageList/{type}")
    @ApiOperation(value = "统一消息消息处理(type:0一键已读1：清空已读消息)")
    public R clearMessageList(@PathVariable("type") Integer type,
                              @RequestBody MessageHandleVo messageHandleVo){
        Long receviceUser = messageHandleVo.getReceviceUser();
        Long projectId = messageHandleVo.getProjectId();
        List<MessageHandleEntity> messageHandleEntities = messageHandleService.selectList(new EntityWrapper<MessageHandleEntity>()
                .eq("recevice_user", receviceUser)
                .eq("is_delete", 0)
                .eq("status", type));

        for(MessageHandleEntity m : messageHandleEntities){
            switch (type){
                case 0:
                    m.setStatus(1);
                    break;
                case 1:
                    m.setIsDelete(1);
                    break;
                default:
                        break;
            }
            messageHandleService.updateById(m);
        }
        //...呵呵
        return R.ok();
    }
}
